package com.tanhua.admin.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.commons.utils.ComputeUtil;
import com.tanhua.model.domain.Analysis;
import com.tanhua.model.vo.AnalysisSummaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class AnalysisService extends ServiceImpl<AnalysisMapper,Analysis> {

    @Autowired(required = false)
    private LogMapper logMapper;

    @Autowired(required = false)
    private AnalysisMapper analysisMapper;

    public void analysis() {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        //1. 查询出来每日注册人数
        Integer numReg = logMapper.queryByTypeAndLogTime("0102",today);

        //2. 查询出来每日登录人数
        Integer numLogin = logMapper.queryByTypeAndLogTime("0101",today);


        //3. 查询出来每日活跃人数
        Integer numActive = logMapper.queryByLogTime(today);

        //4. 查询出来每日次日保留人数
        String yesterday = DateUtil.yesterday().toString("yyyy-MM-dd");
        Integer numRetention1d = logMapper.queryNumRetention1d(today, yesterday);

        //5. 查询日表今天是否存在对应的记录，如果不存在则插入，如果存在则更新
        QueryWrapper<Analysis> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date",today);
        Analysis analysis = analysisMapper.selectOne(queryWrapper);
        if(analysis==null){
//            今日没有记录,则插入
            analysis = new Analysis();
            analysis.setRecordDate(new Date());
            analysis.setNumRegistered(numReg);
            analysis.setNumActive(numActive);
            analysis.setNumLogin(numLogin);
            analysis.setNumRetention1d(numRetention1d);
            analysis.setCreated(new Date());
            analysis.setUpdated(new Date());
//            analysisMapper.insert(analysis);
            this.save(analysis);
        }else{
            //更新
            analysis.setNumRegistered(numReg);
            analysis.setNumActive(numActive);
            analysis.setNumLogin(numLogin);
            analysis.setNumRetention1d(numRetention1d);
            analysis.setUpdated(new Date());
//            analysisMapper.updateById(analysis);
            this.updateById(analysis);
        }

    }

    /**
     * 统计概要信息
     * @return
     */
    public AnalysisSummaryVo getSummary() {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        //1. 累积用户数
        Analysis analysis = query().select("SUM(num_registered) as numRegistered").one();
        Integer numRegistered = analysis.getNumRegistered();
        //2. 查出今日新增、登录、活跃用户数
        Analysis todayAnalysis = query().eq("record_date", today).one();
        Integer todayNumReg = todayAnalysis.getNumRegistered();
        Integer todayNumActive = todayAnalysis.getNumActive();
        Integer todayNumLogin = todayAnalysis.getNumLogin();


        //查询出昨天的新增、登录、活跃用户数
        String yesterday = DateUtil.yesterday().toString("yyyy-MM-dd");
        Analysis yesterdayAnalysis = query().eq("record_date", yesterday).one();
        Integer yesterdayNumReg = yesterdayAnalysis.getNumRegistered();
        Integer yesterdayNumActive = yesterdayAnalysis.getNumActive();
        Integer yesterdayNumLogin = yesterdayAnalysis.getNumLogin();



        //3. 过去7天与30天活跃用户数
        Long day7 =  analysisMapper.findNumActiveByDate(ComputeUtil.offsetDay(new Date(),-7),today);
        Long day30=  analysisMapper.findNumActiveByDate(ComputeUtil.offsetDay(new Date(),-30),today);

        AnalysisSummaryVo vo = new AnalysisSummaryVo();
        vo.setCumulativeUsers(numRegistered.longValue());//累积用户数
        vo.setActivePassMonth(day30); //过去30天的活跃用户数
        vo.setActivePassWeek(day7); //过去7天的活跃用户数
        vo.setNewUsersToday(todayNumReg.longValue()); //今日新增用户数
        vo.setNewUsersTodayRate(ComputeUtil.computeRate(todayNumReg,yesterdayNumReg));
        vo.setLoginTimesToday(todayNumLogin.longValue());
        vo.setLoginTimesTodayRate(ComputeUtil.computeRate(todayNumLogin,yesterdayNumLogin));
        vo.setActiveUsersToday(todayNumActive.longValue()); //活跃用户数
        vo.setActiveUsersTodayRate(ComputeUtil.computeRate(todayNumActive,yesterdayNumActive));

        return vo;
    }
}
